import chalk from 'chalk'
import { resolve } from 'path'
import fs from 'fs'
import createTemplate from './template/components/index.js'

const args = process.argv.slice(2)[0]
const step = (text, color) => console.log(chalk[color](text))

const run = async () => {
  if (!args) {
    step('未获取到生成目录名', 'red')
    return
  }
  const splitPath = args.split('/')
  if (splitPath.length === 1) {
    step('只有父级目录，还需要创建目录名，例：JUI/JInput', 'red')
    return
  }
  const outPath = resolve('packages/' + args)
  fs.stat(outPath, async err => {
    if (err) {
      await fs.mkdirSync(outPath)
      const name = splitPath[1]
      const lowCaseName = splitPath[1].replace('J', '').toLocaleLowerCase()
      for (const i of createTemplate) {
        const p = `${outPath}/${i.fileName}`
        await fs.writeFileSync(p, i.f(name, lowCaseName))
        step(`创建 ${p} 成功`, 'cyan')
      }
    } else {
      step('目录已存在', 'red')
    }
  })
}

run()
